﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using System.Data;
using DAO;
using System.Globalization;
using System.Data.SqlClient;
using System.Configuration;

public partial class Pages_Viewdetails : System.Web.UI.Page
{
    private string entry = "";
    private string catelvid = "";
    string idEntry = "0";
    BUS_WRENTRY busWrentry = new BUS_WRENTRY();
    BUS_WRMEDIASTORE busWrmedia = new BUS_WRMEDIASTORE();
    BUS_WRCITY busWrcity = new BUS_WRCITY();
    BUS_WRCATEGORYLV1 busWrcate = new BUS_WRCATEGORYLV1();
    BUS_WRENTTYPE busWrtype = new BUS_WRENTTYPE();
    public string title = "";
    public string strimage = "";
    public string strimagea = "";
    public string contents = "";
    public string category = "";
    public string typesale = "";
    public string city = "";
    public string giaban = "";
    public string luotxem = "";
    public string ngaydang = "";
    public string thongtin = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (RouteData.Values["idEntry"] != null)
        {
            idEntry = RouteData.Values["idEntry"].ToString();
        }
        if (Convert.ToInt32(idEntry) > 0)
        {
            if (!IsPostBack)
            {
                //string idcity = Session["City"].ToString();
                //catelvid = Session["CateID"].ToString();
                //ShowEntry(idcity, catelvid,1,20);
                ShowEntry(idEntry);
            }
        }
        else
            Response.Redirect("~/Pages/Location.aspx");
    }

    public string str_con = ConfigurationManager.ConnectionStrings["SQlConnection"].ToString();

    private void ShowEntry(string idEntry)
    {
        if (idEntry != "0")
        {
            //lấy tất cả catelv1
            int ID = Int32.Parse(idEntry);

            string sql_up = "Update WRENTRY set Viewed = (Viewed + 1) where ID = " + ID.ToString() + " ";
            SqlConnection oConnection012 = new SqlConnection(str_con);
            oConnection012.Open();
            try
            {
                SqlCommand oCommand011 = new SqlCommand(sql_up, oConnection012);
                oCommand011.ExecuteScalar();
                //id_dangky = Int64.Parse(oCommand.ExecuteScalar().ToString());
            }
            finally
            {
                oConnection012.Close();
                oConnection012.Dispose();
            }

            IList<WRENTRY> iListWRWRCITY = new List<WRENTRY>();
            iListWRWRCITY = busWrentry._GetList(p => p.ID == ID);
            

            DataTable dts = Common.Common.ConvertToDataTable(iListWRWRCITY.ToList());

            if (dts.Rows.Count > 0)
            {
                title = dts.Rows[0]["EntryName"].ToString();
                contents = dts.Rows[0]["EntContent"].ToString();
                giaban = String.Format("{0:0,0}", Convert.ToInt64(dts.Rows[0]["Price"]));
                luotxem = dts.Rows[0]["Viewed"].ToString();
                string ten = dts.Rows[0]["NameToContact"].ToString();
                string diachi = dts.Rows[0]["AddressToContact"].ToString();
                string dienthoai = dts.Rows[0]["PhoneToContact"].ToString();
                string email = dts.Rows[0]["EmailToContact"].ToString();
                string ngay_dang = dts.Rows[0]["CreateDate"].ToString();
                ngaydang = ngay_dang;
                //Int32 viewed = Int32.Parse(dts.Rows[0]["Viewed"].ToString());
                //WRENTRY entry = new WRENTRY();
                //entry.ID = ID;
                //entry.Viewed = viewed + 1;
                //busWrentry._Update(entry);
                //String tungay = convet_ngay(Request["tungay"]);
                //String denngay = convet_ngay(Request["denngay"]);

                ////String tungay = Request["tungay"].ToString().Trim();
                ////String denngay = Request["denngay"].ToString().Trim();

                //DateTime d1 = Convert.ToDateTime(tungay);
                //DateTime d2 = Convert.ToDateTime(denngay);

                //TimeSpan ts = d2.Subtract(d1);

                //double a = ts.TotalDays;

                int idcity = Int32.Parse(dts.Rows[0]["IDCity"].ToString());

                IList<WRCITY> iListWRWRcity = new List<WRCITY>();
                iListWRWRcity = busWrcity._GetList(p => p.ID == idcity);
                DataTable dtsa1 = Common.Common.ConvertToDataTable(iListWRWRcity.ToList());
                if (dtsa1.Rows.Count > 0)
                {
                    city = dtsa1.Rows[0]["CityName"].ToString();
                }
                
                int idcate = Int32.Parse(dts.Rows[0]["IDCategoryLv"].ToString());
                IList<WRCATEGORYLV1> iListWRWRcate = new List<WRCATEGORYLV1>();
                iListWRWRcate = busWrcate._GetList(p => p.ID == idcate);
                DataTable dtsa11 = Common.Common.ConvertToDataTable(iListWRWRcate.ToList());
                if (dtsa11.Rows.Count > 0)
                {
                    category = dtsa11.Rows[0]["CategoryLv1Name"].ToString();
                }

                int idtype = Int32.Parse(dts.Rows[0]["IDEntType"].ToString());
                IList<WRENTTYPE> iListWRWRtype = new List<WRENTTYPE>();
                iListWRWRtype = busWrtype._GetList(p => p.ID == idtype);
                DataTable dtsa12 = Common.Common.ConvertToDataTable(iListWRWRtype.ToList());
                if (dtsa12.Rows.Count > 0)
                {
                    typesale = dtsa12.Rows[0]["EntryTypeName"].ToString();
                }

                IList<WRMEDIASTORE> iListWRWRMEDIA = new List<WRMEDIASTORE>();
                iListWRWRMEDIA = busWrmedia._GetList(p => p.IDEntry == ID);
                DataTable dtsa = Common.Common.ConvertToDataTable(iListWRWRMEDIA.ToList());
                if (dtsa.Rows.Count > 0)
                {
                    string htmls = "";
                    string htmlss = "";
                    for (int i = 0; i < dtsa.Rows.Count; i++)
                    {
                        htmls += "<div> "
                               + "         <img src=\"" + dtsa.Rows[i]["MediaPath"].ToString().Replace("~","..") + "\" alt=\"" + dtsa.Rows[i]["MediaName"].ToString() + "\"  /> "
                               +"     </div>";
                        htmlss += "<span> "
                               + "         <img src=\"" + dtsa.Rows[i]["MediaPath"].ToString().Replace("~", "..") + "\" alt=\"" + dtsa.Rows[i]["MediaName"].ToString() + "\"  /> "
                               + "     </span>";
                    }
                    strimage = htmls;
                    strimagea = htmlss;
                }
                
            }

        }
    }

    public string convet_ngay(string ngay_tmp)
    {
        string[] ngay1 = ngay_tmp.Split('/');
        if (ngay1.Length == 3)
        {
            string ngay11 = "";
            string ngay12 = "";
            if (ngay1[1].ToString().Length == 1) { ngay11 = "0" + ngay1[1].ToString().Trim(); } else { ngay11 = ngay1[1].ToString().Trim(); }
            if (ngay1[0].ToString().Length == 1) { ngay12 = "0" + ngay1[0].ToString().Trim(); } else { ngay12 = ngay1[0].ToString().Trim(); }
            ngay_tmp = ngay11 + "/" + ngay12 + "/" + ngay1[2];
            try
            {
                CultureInfo provider = CultureInfo.InvariantCulture;
                DateTime ngay2;
                ngay2 = DateTime.ParseExact(ngay_tmp, "MM/dd/yyyy", provider);
                return ngay_tmp;
            }
            catch
            {
                return "";
            }
        }
        else
        {
            return "";
        }
    }

}